package com.txc.pay.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.txc.common.global.result.Result;
import com.txc.pay.config.PayConfig;
import com.txc.pay.entity.AccountEntity;
import com.txc.pay.enums.PayMethodEnum;
import com.txc.pay.enums.PayType;
import com.txc.pay.service.IAccountService;

import com.txc.pay.vo.AccountVo;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 支付商户账号配置 前端控制器
 *
 * @author TXC
 * @since 2023-12-27
 */
@RestController
@RequestMapping("/pay/account")
public class AccountController {
    @Resource
    IAccountService accountService;

    /**
     * 商户账号新增或编辑
     *
     * @param accountVo 商户账号信息
     * @param file 密钥文件
     * @return 操作结果
     */
    @PostMapping(value = "/saveOrUpdate", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Result<?> saveOrUpdate(AccountVo accountVo, MultipartFile file) {
        AccountEntity account = new AccountEntity();
        BeanUtils.copyProperties(accountVo, account);
        account = accountService.saveAndUpdate(account, file);
        return Result.ok(account);
    }

    /**
     * 下载密钥文件
     *
     * @param id 账号id
     * @throws IOException 异常捕获
     */
    @GetMapping("/{id}/getPrivateKey")
    public void getPrivateKey(@PathVariable("id") String id, HttpServletResponse response) throws IOException {
        AccountEntity account = accountService.getById(id);
        PayConfig payConfig = PayType.getConfig(account.getPayType(), account.getParam());
        assert payConfig != null;
        response.setHeader("Content-Disposition", "attachment; filename=" + payConfig.getMerchantPrivateKey());
        response.setContentType("application/octet-stream;charset=utf-8");
        OutputStream out = response.getOutputStream();
        out.write(accountService.getPrivateKeyFile(id));
        out.flush();
    }

    /**
     * 删除商户账号
     *
     * @param id 账号id
     * @return 操作结果
     */
    @DeleteMapping("{id}/remove")
    public Result<?> remove(@PathVariable("id") String id) {
        accountService.deleteById(id);
        return Result.ok();
    }

    /**
     * 获取支持的支付方式
     *
     * @return 支付方式
     */
    @GetMapping("/getPayType")
    public Result<?> getPayType() {
        List<Object> data = new ArrayList<>();
        List<PayType> types = PayType.getAll();
        types.forEach(type -> {
            List<Map<String, String>> methods = PayMethodEnum.getEnum(type.getValue());
            Map<String, Object> item = new HashMap<>();
            item.put("type", type.toMap());
            item.put("method", methods);
            data.add(item);
        });
        return Result.ok(data);
    }

    /**
     * 分页查询支付商户账号
     *
     * @param current 当前页码
     * @param size 数量
     * @param userId 用户id
     * @return 账号信息
     */
    @GetMapping("/page")
    public Result<?> getAccountPage(String current, String size, String userId){
        IPage<AccountEntity> page = new Page<>(Long.parseLong(current), Long.parseLong(size));
        return  Result.ok(accountService.getAccountPage(page, userId));
    }

}

